<?php 
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$option = JRequest::getCmd('option');

JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');

$doc =& JFactory::getDocument();
$doc->addScriptDeclaration('function jSelectUser_users(id, title) {var old_id = document.getElementById("users").value;if (old_id != id) {document.getElementById("users").value = id;document.getElementById("users_name").value = title;}SqueezeBox.close();}');
JHTML::_('behavior.modal', 'input.select_users', array('handler' => 'ajax'));
?>

<script type="text/javascript">
	function calculateWebCharge()
	{
		var result = 0;
	
		var tax = $('#jform_tax').val();
		tax = tax.length == 0 ? 0 : tax;
		
		var shipus = $('#jform_shipus').val();
		shipus = shipus.length == 0 ? 0 : shipus;
		
		var webprice = $('#jform_usprice').val();
		webprice = webprice.length == 0 ? 0 : webprice;
		
		var quantity = $('#jform_quantity').val();
		quantity = quantity.length == 0 ? 0 : quantity;
		
		result = parseFloat(webprice) + (parseFloat(webprice) * parseFloat(tax) / 100) + parseFloat(shipus);
		result *= parseFloat(quantity);
		//result = Math.round(result).toFixed(2);
		
		return result;
	}

	function calculateFee(webcharge)
	{
		var level1 = $('#fee_level1').val();
		var level2 = $('#fee_level2').val();
		var level3 = $('#fee_level3').val();
		
		if (webcharge < level1)
			return $('#fee_level1_val').val();
		else if (webcharge >= level1 && webcharge < level2)
			return $('#fee_level2_val').val();
		else if (webcharge >= level2 && webcharge < level3)
			return $('#fee_level3_val').val();
	}
	
	function calculateVnd()
	{
		var webcharge = $('#jform_webcharge').val();
		webcharge = webcharge.length == 0 ? 0 : webcharge;
		var currency = $('#jform_currency').val();
		var surcharge = $('#jform_surcharge').val();
		surcharge = surcharge.length == 0 ? 0 : surcharge;
		var result = (parseFloat(webcharge) + parseFloat(surcharge)) * parseFloat(currency);
		
		$('#jform_vnprice').val(result);
		$('#jform_roundvnd').val(numeral(result).format('0,0'));
	}
	
	window.addEvent('domready', function(){
			$('#jform_roundvnd').val($('#jform_vnprice').val());
	
			$('#jform_tax').keyup(function(event)
			{
				$('#jform_webcharge').val(calculateWebCharge());
				calculateVnd();
			});

			$('#jform_tax').keydown(function(event)
			{
				handleDigit(event);
			});

			$('#jform_shipus').keyup(function(event)
			{
				$('#jform_webcharge').val(calculateWebCharge());
				calculateVnd();
			});

			$('#jform_shipus').keydown(function(event)
			{
				handleDigit(event);
			});

			$('#jform_usprice').keyup(function(event)
			{
				$('#jform_webcharge').val(calculateWebCharge());
				calculateVnd();
			});

			$('#jform_usprice').keydown(function(event)
			{
				handleDigit(event);
			});

			$('#jform_quantity').keyup(function(event)
			{
				$('#jform_webcharge').val(calculateWebCharge());
				calculateVnd();
			});

			$('#jform_quantity').keydown(function(event)
			{
				handleDigit(event);
			});

			$('#jform_webcharge').click(function(event)
			{
				$('#jform_webcharge').val(calculateWebCharge());
				calculateVnd();
			});
			
			$('#jform_surcharge').keyup(function(event)
			{
				$('#jform_vnprice').val(calculateVnd());
			});

			$('#jform_surcharge').keydown(function(event)
			{
				handleDigit(event);
			});
			$('#jform_vnprice').click(function(event)
			{
				calculateVnd();
			});
			
		});
</script>

<form action="index.php" method="post" name="adminForm"
	id="order-admin-form" class="form-validate">
	<input type="hidden" name="option" value="<?php echo $option; ?>" />
	<input
		type="hidden" name="task" value="" />
		<input type="hidden" name="id"
		value="<?php echo $this->item->id; ?>" />
	<?php echo JHtml::_('form.token'); ?>

	<fieldset class="adminform">
		<legend>
			<?php echo JText::_( 'DETAILS' ); ?>
		</legend>
		<ul class="adminformlist">
			<?php foreach ($this->form->getFieldset() as $field)
			{
				?>
			<li><?php echo $field->label; ?> <?php echo $field->input; ?></li>
			<?php
			}
			?>
		</ul>
	</fieldset>
</form>
